package lt.inkredibl.iit;

import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.util.List;

public class Tetragon
{
	private ArrayList<Edge> _edges = new ArrayList<Edge>(4);

	public void addEdge(List<Point2D> edge)
	{
		if (_edges.size() < 4)
		{
			_edges.add(Edge.valueOf(edge));
		}
		else
		{
			throw new IllegalStateException("Already four edges were present!");
		}
		
		if (_edges.size() == 4)
		{
			normalize();
		}
	}

	public void normalize()
	{
		double vv = _edges.get(0).verticality() + _edges.get(2).verticality();
		double hv = _edges.get(1).verticality() + _edges.get(3).verticality();
		if (vv < hv)
		{
			_edges.add(_edges.remove(0));
		}
		
	}
	
	public double getWidth()
	{
		return (_edges.get(1).length() + _edges.get(3).length()) / 2;
	}
	
	public double getHeight()
	{
		return (_edges.get(0).length() + _edges.get(2).length()) / 2;
	}

	public Edge getEdge(int i)
	{
		return _edges.get(i);
	}

	public Edge getLeftEdge()
	{
		return getEdge(0);
	}
	
	public Edge getTopEdge()
	{
		return getEdge(1);
	}
	
	public Edge getRightEdge()
	{
		return getEdge(2);
	}
	
	public Edge getBottomEdge()
	{
		return getEdge(3);
	}

	public Rectangle2D bounds()
	{
		Rectangle2D bounds = getLeftEdge().getBounds();
		bounds.add(getTopEdge().getBounds());
		bounds.add(getRightEdge().getBounds());
		bounds.add(getBottomEdge().getBounds());
		return bounds;
	}
}
